<template>
    <div id="app">
      <el-form label-width="100px">
        <el-form-item label="类别">
          <el-select v-model="secondhandBook.cid" placeholder="请选择">
            <el-option
              v-for="item in categoryList"
              :key="item.categoryId"
              :label="item.categoryName"
              :value="item.categoryId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="书名">
          <el-input v-model="secondhandBook.bookName"></el-input>
        </el-form-item>
        <el-form-item label="原价">
          <el-input v-model="secondhandBook.bookPrice"></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="secondhandBook.author"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="secondhandBook.description"  type="textarea" rows="5"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            class="upload-demo"
            action="http://localhost:8989/secondhandBook/upload"
            :limit="1"
            :on-exceed="handleOnExceed"
            name="pic"
            accept=".png,.jpg"
            :multiple="false"
            :on-success="handleOnSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveSecondhandBook()">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>

<script>
    export default {
      data(){
        return {
          secondhandBook:{},
          categoryList:[]
        }
      },
      created() {
        this.axios.get("/category/selectAll").then((resp)=>{
          this.categoryList=resp.data;
        });
      },
      methods:{
        handleOnExceed(){
          console.log("文件个数超出限制");
        },
        handleOnSuccess(resp){
          //文件上传成功之后要把文件上传到哪传给brand对象
          this.secondhandBook.productImage=resp;
        },
        saveSecondhandBook(){
          //发出ajax请求
          this.axios.post("/secondhandBook/add",this.secondhandBook).then((resp)=>{
            this.$router.push("/book/SecondhandList");
          })
        }
      }
    }
</script>


<style scoped>

</style>
